package com.lqr.wechat.activity;

import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import com.bridge.imconfig.NimUserInfoSDK;
import com.lqr.wechat.R;
import com.lqr.wechat.databinding.ActivityChangeNameBinding;
import com.netease.nimlib.sdk.RequestCallbackWrapper;
import com.netease.nimlib.sdk.uinfo.constant.UserInfoFieldEnum;
import java.util.HashMap;
import java.util.Map;

/**
 * @创建者 CSDN_LQR
 * @描述 更改名字
 */
public class ChangeNameActivity extends BaseActivity {
    private String mName;
    private ActivityChangeNameBinding binding ;
    @Override
    protected int layoutId() {
        return R.layout.activity_change_name;
    }

    @Override
    public void init() {
        mName = getIntent().getStringExtra("name");
    }

    @Override
    public void initListener() {
        binding.lyToolbar.btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = binding.etName.getText().toString();
                if (TextUtils.isEmpty(name.trim())) {
                    showMaterialDialog("提示", "没有输入昵称，请重新填写", "确定", "", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            hideMaterialDialog();
                        }
                    }, null);
                } else {
                    showWaitingDialog("请稍等");
                    Map<UserInfoFieldEnum, Object> fields = new HashMap<>(1);
                    fields.put(UserInfoFieldEnum.Name, name);
                    NimUserInfoSDK.updateUserInfo(fields, new RequestCallbackWrapper<Void>() {
                        @Override
                        public void onResult(int code, Void result, Throwable exception) {
                            hideWaitingDialog();
                            finish();
                        }
                    });
                }
            }
        });
    }

    @Override
    public void initView() {
        binding = getBind() ;
        initToolbar();
        binding.etName.setText(mName);
        binding.etName.setSelection(mName.length());
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initToolbar() {
        setSupportActionBar(binding.lyToolbar.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("更改名字");
        binding.lyToolbar.toolbar.setNavigationIcon(R.mipmap.ic_back);
        binding.lyToolbar.btnOk.setVisibility(View.VISIBLE);
    }
}
